/*
 * ConfigureJSM.java
 *
 * Created on 14 de junio de 2007, 17:29
 */

package net.javahispano.jsignalwb.ui;

import java.awt.Color;
import java.awt.Window;
import java.beans.*;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Date;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.javahispano.jsignalwb.JSWBManager;
import net.javahispano.jsignalwb.Signal;
import net.javahispano.jsignalwb.jsignalmonitor.TimeRepresentation;
import org.joda.time.DateTime;

/**
 *
 * @author  Roman
 */
public class ConfigureJSM extends javax.swing.JPanel implements PropertyChangeListener, DocumentListener {
    private JSWBManager jswbManager;
    private boolean zoomH;
    private boolean scroll;
    /** Creates new form ConfigureJSM */
    public ConfigureJSM(JSWBManager jswbManager) {
        this.jswbManager = jswbManager;
        initComponents();
        scroll = zoomH = false;
        jTextFieldDate1.setText(TimeRepresentation.timeToString(
                calcularFechaBase()));
        datePicker1.showButtonOnly(true);
        try {
            datePicker1.setDate(new Date(jswbManager.getJSMScrollValue()));
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
        datePicker1.addPropertyChangeListener(datePicker1.PROPERTY_NAME_DATE, this);
        applyButton.setEnabled(false);
        initPropertiesListeners();
        cancelButton.grabFocus();
    }

    /**
     * calcularFechaBase
     *
     * @return long
     */
    private long calcularFechaBase() {
       Collection <Signal> signals = jswbManager.getSignalManager().getSignals();
       Signal cualquierSignal= (Signal)signals.toArray()[0];
       return cualquierSignal.getStart();
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ("date".equals(evt.getPropertyName())) {
            jTextFieldDate1.setText(TimeRepresentation.timeToString(
                    swapDateNoChangeTime(jswbManager.getJSMScrollValue(),
                                         datePicker1.getDate().getTime())));
        }
    }

    public void changedUpdate(DocumentEvent evt) {
        documentEvent(evt);
    }

    public void removeUpdate(DocumentEvent evt) {
        documentEvent(evt);
    }

    public void insertUpdate(DocumentEvent evt) {
        documentEvent(evt);
    }

    private void initPropertiesListeners() {
        scroll = zoomH = false;
        jTextField1.getDocument().addDocumentListener(this);
        jTextFieldDate1.getDocument().addDocumentListener(this);
        jTextField1.getDocument().putProperty("textField", "zoomH");
        jTextFieldDate1.getDocument().putProperty("textField", "scroll");
    }

    private void documentEvent(DocumentEvent evt) {
        String property = evt.getDocument().getProperty("textField").toString();

        if (property.equals("scroll")) {
            scroll = true;
        } else if (property.equals("zoomH")) {
            zoomH = true;
        }
        evt.getDocument().removeDocumentListener(this);
        checkApplyButton();
    }

    private void checkApplyButton() {
        if (scroll || zoomH) {
            applyButton.setEnabled(true);
        } else {
            applyButton.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTextField1 = new javax.swing.JTextField();
        jSlider1 = new javax.swing.JSlider();
        okButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDate1 = new net.javahispano.jsignalwb.ui.JTextFieldDate();
        datePicker1 = new com.michaelbaranov.microba.calendar.DatePicker();
        jLabel2 = new javax.swing.JLabel();

        jTextField1.setText(Float.toString(jswbManager.getJSMFrecuency()));

        jSlider1.setMajorTickSpacing(100);
        jSlider1.setMaximum(0);
        jSlider1.setMaximum(100);
        jSlider1.setMinorTickSpacing(1);
        jSlider1.setPaintTicks(true);
        jSlider1.setSnapToTicks(true);
        jSlider1.setValue((int) (jswbManager.getJSMFrecuency() * 10));
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Frecuency:");

        jLabel2.setText("Scroll Value:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addGroup(layout.createSequentialGroup()
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
                LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addGap(67, 67, 67)
                          .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(applyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addGap(80, 80, 80))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                          .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                              .addComponent(jTextFieldDate1, javax.swing.GroupLayout.PREFERRED_SIZE,
                152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addComponent(datePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                              .addContainerGap())))
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addGap(39, 39, 39)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                              .addComponent(jLabel2)
                                              .addComponent(jTextFieldDate1, javax.swing.GroupLayout.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(datePicker1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1)
                                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(okButton)
                                    .addComponent(applyButton)
                                    .addComponent(cancelButton))
                          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
    } // </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_okButtonActionPerformed
        if (apply()) {
            hideJWindow();
            jswbManager.refreshJSM(false);
        }
    } //GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_cancelButtonActionPerformed
        hideJWindow();
        jswbManager.refreshJSM(false);
    } //GEN-LAST:event_cancelButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_applyButtonActionPerformed
        apply();
    } //GEN-LAST:event_applyButtonActionPerformed

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) { //GEN-FIRST:event_jSlider1StateChanged
        if (jSlider1.getValue() >= 0) {
            double fs = 0.00999 * jSlider1.getValue() + 0.001;
            DecimalFormat df = new DecimalFormat("#.####");
            String texto = df.format(fs);

            System.out.println("" + jSlider1.getValue() + " fs " + fs);
            texto = texto.replace(',', '.');
            jTextField1.setText(texto);
        }
        // jTextField1.setText(Float.toString(jSlider1.getValue()/10f));
        else {
            jTextField1.setText("0.1");
        }
    } //GEN-LAST:event_jSlider1StateChanged

    private boolean apply() {
        boolean flag = true;
        if (zoomH) {
            try {
                float value = Float.valueOf(jTextField1.getText());
                jswbManager.setJSMFrecuency(value);
                jswbManager.refreshJSM(true);
                jTextField1.setBackground(Color.WHITE);
                flag = true;
            } catch (NumberFormatException ex) {
                jTextField1.setBackground(Color.RED);
            }
        } else if (scroll) {
            String dateValue = jTextFieldDate1.getFormattedText();
            if (!dateValue.equals("")) {
                try {
                    long newScroll = TimeRepresentation.stringToMillis(dateValue, true, true, true);
                   // if (newScroll >= jswbManager.getJSMScrollBaseTime() && newScroll <= jswbManager.getJSMMaxTime()) {
                       Collection <Signal> signals = jswbManager.getSignalManager().getSignals();
                       boolean b []= new boolean[signals.size()];
                       int c=0;
                       for (Signal s : signals) {
                           s.setStart(newScroll);
                           if (s.isVisible()) {
                               b[c]= true;
                           }
                           else {
                                b[c]= false;
                           }
                           c++;
                       }
                       c=0;
                       for (Signal s : signals) {
                           s.setVisible(false);
                           s.setVisible(b[c]);
                           c++;
                       }
                        jTextFieldDate1.setBackground(Color.WHITE);
                   /* } else {
                        jTextFieldDate1.setBackground(Color.RED);
                    }*/
                } catch (Exception ex) {
                    jTextFieldDate1.setBackground(Color.RED);
                    flag = false;
                }
            } else {
                jTextFieldDate1.setBackground(Color.RED);
                flag = false;
            }
        }
        return flag;
    }

    public void showJWindow(Window owner) {
        jw = new JWindow(owner);
        jw.add(this);
        jw.setSize(this.getPreferredSize());
        jw.setLocationRelativeTo(owner);
        jw.setVisible(true);
    }

    private void hideJWindow() {
        jw.dispose();
    }

    /**
     * @param old long
     * @param newTime long
     * @return long
     */
    private static long swapDateNoChangeTime(long old, long newTime) {
        DateTime dt = new DateTime(old);
        DateTime newDateTime = new DateTime(newTime).withTime(dt.getHourOfDay(),
                dt.getMinuteOfHour(), dt.getSecondOfMinute(),
                dt.getMillisOfSecond());

        return newDateTime.getMillis();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton applyButton;
    private javax.swing.JButton cancelButton;
    private com.michaelbaranov.microba.calendar.DatePicker datePicker1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    private net.javahispano.jsignalwb.ui.JTextFieldDate jTextFieldDate1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    private JWindow jw;
}
